const Koa = require('koa')
const fs = require('fs')
const app = new Koa()

const router = require('koa-router')()

const getAllCoin=require('../../controller/coinController').Coinall //所有币种
const getallMine=require('../../controller/coinController').allMine //获取用户余额

const Gamelist=require('../../controller/gameController').Gamelist //游戏列表
const Gamerecommend=require('../../controller/gameController').Gamerecommend //游戏推荐
const pcGamerecommend=require('../../controller/gameController').pcGamerecommend //PC端首页游戏推荐
const pcbaccaratv=require('../../controller/gameController').pcbaccaratv //PC端 百家乐

const getUserBasicInfo=require('../../controller/userController').userBasicInfo //用户信息

const getAllannouncement=require('../../controller/Bulletin').getAllannouncement //请求最新八条公告
const queryAnnouncementById=require('../../controller/Bulletin').queryAnnouncementById //根据公告ID查询详情

const defaultsabahlogin=require('../../controller/sabahcontroller').sabahlogin //默认沙巴登录

const home=router.get('/', async ( ctx )=>{  //主页
    let useragent=ctx.headers['user-agent'].toLowerCase()
    let ismobile = /(iphone|ipod|ipad|android)/.test(useragent)
    if(ismobile){
        ctx.redirect('/m');
        ctx.status = 301
    }
    const gamelist=await Gamelist() //游戏列表
    const gamerecommend=await pcGamerecommend() //pc端老虎机游戏推荐
    const baccaratv=await pcbaccaratv() //PC端 百家乐
    const allcoin=await getAllCoin() //所有币种
    const allMine=await getallMine(ctx) //获取用户余额
    const allannouncement=await getAllannouncement(ctx) //请求最新八条公告
    const userbasicinfo=await getUserBasicInfo(ctx) //获取用户信息
    let lastFaucetTime=ctx.session.lastFaucetTime
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    await ctx.render('home',{
        allcoin,
        allMine,
        gamelist,
        gamerecommend,
        baccaratv,
        lang,
        lastFaucetTime,
        allannouncement,
        userbasicinfo
    })
})
const LotteryTicket=router.get('/LotteryTicket',async (ctx) => { //区块彩票
    const allMine=await getallMine(ctx) //获取用户余额
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let lastFaucetTime=ctx.session.lastFaucetTime
    await ctx.render('lottery',{
        lang,
        allMine,
        lastFaucetTime
    })
})

const login=router.get('/singin',async (ctx) => { //登录
    const allMine=await getallMine(ctx) //获取用户余额
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let lastFaucetTime=ctx.session.lastFaucetTime
    await ctx.render('login',{
        lang,
        allMine,
        lastFaucetTime
    })
})

const registered=router.get('/registered',async (ctx) => { //注册
    const allMine = await getallMine(ctx)
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let lastFaucetTime=ctx.session.lastFaucetTime
    await ctx.render('registered',{
        lang,
        allMine,
        lastFaucetTime
    })
})

const growth=router.get('/growth',async (ctx) => { //个人中心
    const allMine=await getallMine(ctx) //获取用户余额
    const userbasicinfo=await getUserBasicInfo(ctx) //获取用户信息
    let lastFaucetTime=ctx.session.lastFaucetTime语言
    let lang=ctx.cookies.get('lang')||'zh_CN' //
    await ctx.render('growth',{
        lang,
        allMine,
        userbasicinfo,
        lastFaucetTime
    })
})
const transaction = router.get("/transaction/:id", async ctx => {
    const allMine = await getallMine(ctx)
    const userbasicinfo=await getUserBasicInfo(ctx) //获取用户信息
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let lastFaucetTime=ctx.session.lastFaucetTime
    await ctx.render("transaction",{
        allMine,
        lang,
        lastFaucetTime,
        userbasicinfo
    })
})

const Details=router.get('/Details/:id',async (ctx) => { //公告详情
    const allMine = await getallMine(ctx)
    const announcementById= await queryAnnouncementById(ctx) //根据公告ID查询详情
    const userbasicinfo=await getUserBasicInfo(ctx) //获取用户信息
    let lastFaucetTime=ctx.session.lastFaucetTime
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    await ctx.render('Details',{
        lang,
        allMine,
        lastFaucetTime,
        announcementById,
        userbasicinfo
    })
})
const announcement=router.get('/announcement',async (ctx) => { //新闻公告
    const allMine = await getallMine(ctx)
    const userbasicinfo=await getUserBasicInfo(ctx) //获取用户信息
    const allannouncement=await getAllannouncement(ctx) //请求最新八条公告
    let lastFaucetTime=ctx.session.lastFaucetTime
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    await ctx.render('announcement',{
        lang,
        allMine,
        lastFaucetTime,
        userbasicinfo,
        allannouncement
    })
})

const moregame=router.get('/moregame',async (ctx) => { //更多游戏
    const allMine=await getallMine(ctx) //获取用户余额
    const gamerecommend=await Gamerecommend() //AG游戏推荐
    const userbasicinfo=await getUserBasicInfo(ctx) //获取用户信息
    const baccaratv=await pcbaccaratv() //PC端 百家乐
    let lastFaucetTime=ctx.session.lastFaucetTime
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    await ctx.render('moregame',{
        lang,
        allMine,
        lastFaucetTime,
        gamerecommend,
        baccaratv,
        userbasicinfo
    })
})

const forget=router.get('/forget',async(ctx)=>{
    const allMine = await getallMine(ctx)
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let lastFaucetTime=ctx.session.lastFaucetTime
    await ctx.render('forget',{
        lang,
        allMine,
        lastFaucetTime
    })
})

const mhome = router.get('/m',async(ctx) => {
    let useragent=ctx.headers['user-agent'].toLowerCase()
    let ismobile = /(iphone|ipod|ipad|android)/.test(useragent)
    if(/(micromessenger)/.test(useragent)){
        if(!ctx.query.ref){
            ctx.redirect('/m/skip');
        }else{
            ctx.redirect(`/m/skip?ref=${parseInt(ctx.query.ref)}`);
        }
        ctx.status = 301
    }
    if(!ismobile){
        ctx.redirect('/');
        ctx.status = 301
    }
    const gamelist=await Gamelist() //游戏列表
    const gamerecommend=await Gamerecommend() //游戏推荐
    const allcoin=await getAllCoin() //所有币种
    const allMine=await getallMine(ctx) //获取用户余额
    const userbasicinfo=await getUserBasicInfo(ctx) //获取用户信息
    // const allannouncement=await getAllannouncement(ctx) //请求最新八条公告
    let lastFaucetTime=ctx.session.lastFaucetTime
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let title=ctx.i18n.locales[ctx.i18n.locale].Home
    await ctx.render('mhome',{
        lang,
        allMine,
        allcoin,
        userbasicinfo,
        title,
        lastFaucetTime,
        gamelist,
        gamerecommend
    })
})

const skip=router.get("/m/skip",async (ctx) => { //登录
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let useragent=ctx.headers['user-agent'].toLowerCase()
    if(!(/(micromessenger)/.test(useragent))){
        if(!ctx.query.ref){
            ctx.redirect('/m');
        }else{
            ctx.redirect(`/m?ref=${parseInt(ctx.query.ref)}`);
        }
        ctx.status = 301
    }
    await ctx.render('skip',{
        lang
    })
})

const mmoregame = router.get('/m/moregame',async(ctx) => { //更多AG亚游
    const gamerecommend=await Gamerecommend() //游戏推荐
    const allcoin=await getAllCoin() //所有币种
    const allMine=await getallMine(ctx) //获取用户余额
    const userbasicinfo=await getUserBasicInfo(ctx) //获取用户信息
    let lastFaucetTime=ctx.session.lastFaucetTime
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let title=ctx.i18n.locales[ctx.i18n.locale].moregame
    await ctx.render('mmoregame',{
        title,
        lang,
        allMine,
        allcoin,
        userbasicinfo,
        lastFaucetTime,
        gamerecommend
    })
})


const mlogin=router.get('/m/singin',async (ctx) => { //登录
    const allMine=await getallMine(ctx) //获取用户余额
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    const allcoin=await getAllCoin() //所有币种
    const userbasicinfo=await getUserBasicInfo(ctx) //获取用户信息
    let title=ctx.i18n.locales[ctx.i18n.locale].singin
    await ctx.render('mlogin',{
        title,
        lang,
        allMine,
        allcoin,
        userbasicinfo
    })
})

const personal=router.get('/m/personal',async (ctx) => { //个人资料
    const allcoin=await getAllCoin() //所有币种
    const allMine=await getallMine(ctx) //获取用户余额
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    const userbasicinfo=await getUserBasicInfo(ctx)
    let lastFaucetTime=ctx.session.lastFaucetTime
    let title=ctx.i18n.locales[ctx.i18n.locale].PersonalCenter
    await ctx.render('personal',{
        lang,
        allMine,
        lastFaucetTime,
        allcoin,
        userbasicinfo,
        title
    })
})
const Promotionpage=router.get('/m/Promotionpage',async (ctx) => { //推广
    const allcoin=await getAllCoin() //所有币种
    const allMine=await getallMine(ctx) //获取用户余额
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    const userbasicinfo=await getUserBasicInfo(ctx)
    let lastFaucetTime=ctx.session.lastFaucetTime
    let title=ctx.i18n.locales[ctx.i18n.locale].extensionDetail
    await ctx.render('Promotionpage',{
        lang,
        allMine,
        lastFaucetTime,
        allcoin,
        userbasicinfo,
        title
    })
})

const promotiondetail=router.get('/m/promotiondetail/:coin',async (ctx) => { //推广
    const allcoin=await getAllCoin() //所有币种
    const allMine=await getallMine(ctx) //获取用户余额
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    const userbasicinfo=await getUserBasicInfo(ctx)
    let lastFaucetTime=ctx.session.lastFaucetTime
    let title=ctx.i18n.locales[ctx.i18n.locale].extensionDetail
    await ctx.render('promotiondetail',{
        lang,
        allMine,
        lastFaucetTime,
        allcoin,
        userbasicinfo,
        title
    })
})

const mregistered=router.get('/m/registered',async (ctx) => { //注册
    const allMine = await getallMine(ctx)
    const allcoin=await getAllCoin() //所有币种
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let lastFaucetTime=ctx.session.lastFaucetTime
    const userbasicinfo=await getUserBasicInfo(ctx)
    let title=ctx.i18n.locales[ctx.i18n.locale].Registered
    await ctx.render('mregistered',{
        title,
        lang,
        allMine,
        lastFaucetTime,
        allcoin,
        userbasicinfo
    })
})

const mforget=router.get('/m/forget',async(ctx)=>{
    const allcoin=await getAllCoin() //所有币种
    const allMine = await getallMine(ctx)
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let lastFaucetTime=ctx.session.lastFaucetTime
    const userbasicinfo=await getUserBasicInfo(ctx)
    let title=ctx.i18n.locales[ctx.i18n.locale].forgetpassword
    await ctx.render('mforget',{
        title,
        lang,
        allMine,
        lastFaucetTime,
        allcoin,
        userbasicinfo
    })
})

const mgrowth=router.get('/m/growth',async (ctx) => { //个人中心
    const allMine=await getallMine(ctx) //获取用户余额
    const allcoin=await getAllCoin() //所有币种
    const userbasicinfo=await getUserBasicInfo(ctx)
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let lastFaucetTime=ctx.session.lastFaucetTime
    let title=ctx.i18n.locales[ctx.i18n.locale].Management
    await ctx.render('mgrowth',{
        lang,
        allcoin,
        allMine,
        userbasicinfo,
        lastFaucetTime,
        title
    })
})

const mdeposit=router.get('/m/deposit/:id',async (ctx) => { //充值
    const allMine = await getallMine(ctx)
    let lastFaucetTime=ctx.session.lastFaucetTime
    const allannouncement=await getAllannouncement(ctx)
    const userbasicinfo=await getUserBasicInfo(ctx)
    const allcoin=await getAllCoin() //所有币种
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let title=ctx.i18n.locales[ctx.i18n.locale].recharge
    await ctx.render('mdeposit',{
        lang,
        allMine,
        lastFaucetTime,
        allcoin,
        allannouncement,
        userbasicinfo,
        title
    })
})

const mtransfer=router.get('/m/transfer/:id',async (ctx) => { //充值
    const allMine = await getallMine(ctx)
    let lastFaucetTime=ctx.session.lastFaucetTime
    const allannouncement=await getAllannouncement(ctx)
    const userbasicinfo=await getUserBasicInfo(ctx)
    const allcoin=await getAllCoin() //所有币种
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let title=ctx.i18n.locales[ctx.i18n.locale].transfer
    await ctx.render('mtransfer',{
        lang,
        allMine,
        lastFaucetTime,
        allcoin,
        allannouncement,
        userbasicinfo,
        title
    })
})

const mwithdraw=router.get("/m/withdraw/:id",async (ctx) => { //登录
    const allcoin=await getAllCoin() //所有币种
    const allMine=await getallMine(ctx) //获取用户余额
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    const userbasicinfo=await getUserBasicInfo(ctx)
    let lastFaucetTime=ctx.session.lastFaucetTime
    let title=ctx.i18n.locales[ctx.i18n.locale].withdraw
    await ctx.render('mwithdraw',{
        lang,
        allMine,
        lastFaucetTime,
        allcoin,
        userbasicinfo,
        title
    })
})

const mannouncement=router.get('/m/announcement',async (ctx) => { //新闻公告
    const allMine = await getallMine(ctx)
    let lastFaucetTime=ctx.session.lastFaucetTime
    const allannouncement=await getAllannouncement(ctx)
    const userbasicinfo=await getUserBasicInfo(ctx)
    const allcoin=await getAllCoin() //所有币种
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let title=ctx.i18n.locales[ctx.i18n.locale].Newsannouncement
    await ctx.render('mannouncement',{
        lang,
        allMine,
        lastFaucetTime,
        allcoin,
        allannouncement,
        userbasicinfo,
        title
    })
})

const mDetails=router.get('/m/Details/:id',async (ctx) => { //公告详情
    const allMine = await getallMine(ctx)
    const allcoin=await getAllCoin() //所有币种
    const announcementById= await queryAnnouncementById(ctx) //根据公告ID查询详情
    let lastFaucetTime=ctx.session.lastFaucetTime
    const userbasicinfo=await getUserBasicInfo(ctx)
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let title=ctx.i18n.locales[ctx.i18n.locale].Newsdetails
    await ctx.render('mDetails',{
        lang,
        allMine,
        lastFaucetTime,
        announcementById,
        userbasicinfo,
        allcoin,
        title
    })
})
const mshareit=router.get('/m/shareit',async (ctx) => { //公告详情
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let title=ctx.i18n.locales[ctx.i18n.locale].Newsdetails
    await ctx.render('mshareit',{
        lang,
    })
})

const PicksPal=router.get('/PicksPal',async (ctx) => { //PC端体育彩票
    const allMine=await getallMine(ctx) //获取用户余额
    const userbasicinfo=await getUserBasicInfo(ctx) //获取用户信息
    // const defsabalogin=await defaultsabahlogin(ctx) //默认沙巴登录
    let lastFaucetTime=ctx.session.lastFaucetTime
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    let title=ctx.i18n.locales[ctx.i18n.locale].PicksPal
    await ctx.render('PicksPal',{
        // defsabalogin,
        lang,
        allMine,
        lastFaucetTime,
        title,
        userbasicinfo
    })
})

const mPicksPal = router.get('/m/PicksPal',async(ctx) => { //移动端
    const defsabalogin=await defaultsabahlogin(ctx) //默认沙巴登录
    let title=ctx.i18n.locales[ctx.i18n.locale].PicksPal //title描述文字
    await ctx.render('mPicksPal',{
        defsabalogin,
        title
    })
})

const applicationhtml=router.get('/application',async (ctx) => { //上币申请
    const allMine=await getallMine(ctx) //获取用户余额
    const userbasicinfo=await getUserBasicInfo(ctx) //获取用户信息
    let lastFaucetTime=ctx.session.lastFaucetTime
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    await ctx.render('application',{
        lang,
        allMine,
        lastFaucetTime,
        userbasicinfo
    })
})

const tcggame=router.get('/tcggame',async (ctx) => { //TCG彩票
    let title=ctx.i18n.locales[ctx.i18n.locale].modeltitle2
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    const allMine=await getallMine(ctx) //获取用户余额
    await ctx.render('tcggame',{
        title,
        lang,
        allMine
    })
})

const mtcggame=router.get('/m/tcggame',async (ctx) => { //TCG彩票移动端
    let title=ctx.i18n.locales[ctx.i18n.locale].mmodeltitle2 
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    const allMine=await getallMine(ctx) //获取用户余额
    const tcgmobilegame=require('../../controller/tcgcontroller').tcgmobilegame
    await ctx.render('mtcggame',{
        title,
        lang,
        allMine,
        tcgmobilegame
    })
})

const mtcgexchange=router.get('/m/mtcgexchange',async (ctx) => { //TCG彩票兑换
    let title=ctx.i18n.locales[ctx.i18n.locale].mmodeltitle2 
    let lang=ctx.cookies.get('lang')||'zh_CN' //语言
    const allMine=await getallMine(ctx) //获取用户余额
    await ctx.render('mtcgexchange',{
        title,
        lang,
        allMine
    })
})

const mtcggameurl=router.get('/m/mtcggameurl',async (ctx) => { //移动端TCG彩票进入游戏
    let title=ctx.i18n.locales[ctx.i18n.locale].mmodeltitle2 
    let tcgurl=ctx.session.tcggameurl
    await ctx.render('mtcggameurl',{
        title,
        tcgurl
    })
})

module.exports=router